<template>
  <div class="full-box padding-box shadow-box">
    <!-- <div> -->
    <OperateCommon :showBack="true" @back="goBack"> </OperateCommon>
    <el-collapse v-model="activeNames" :accordion="false">
      <el-collapse-item
        v-for="item in items"
        :key="item.key"
        :title="item.title"
        :name="item.key"
      >
        <div v-if="detail">
          <template v-if="item.key == 0">
            <div class="desc">
              <label>项目名称：</label>
              <span>{{ detail.project_name }}</span>
            </div>
            <div class="desc">
              <label>建设单位：</label>
              <span>{{ detail.owner_name }}</span>
            </div>
            <div class="desc">
              <label>项目地址：</label>
              <span>{{ detail.project_address }}</span>
            </div>
            <div class="desc">
              <label>建设内容及规模：</label>
              <span>{{ detail.project_content }}</span>
            </div>
            <div class="desc">
              <label>总投资：</label>
              <span>{{ detail.investment }} （ 万元 ）</span>
            </div>
            <div class="desc">
              <label>开工日期：</label>
              <span>{{ detail.fact_start_date }}</span>
            </div>
            <div class="desc">
              <label>竣工日期：</label>
              <span>{{ detail.accept_date }}</span>
            </div>
          </template>

          <template v-if="item.key == 1">
            <template v-if="pageType == 0 && detail.status_text == '退回'">
              <el-form label-width="150px" :inline="false">
                <div class="d-row">
                  <el-form-item label="附件（ 自评报告 ）" prop="files">
                    <CommonUpload
                      ref="upload"
                      :pid="detail.id"
                      tableName="performanceSelfAssessment"
                      useType="Self-Assessment-Report"
                    ></CommonUpload>
                  </el-form-item>
                </div>
                <div class="d-row">
                  <el-form-item class="d-textarea" label="备注" prop="">
                    <el-input
                      type="textarea"
                      v-model="detail.remark"
                      placeholder=""
                      clearable
                      :autosize="{
                        minRows: 1,
                        maxRows: 5,
                      }"
                    ></el-input>
                  </el-form-item>
                </div>
                <el-form-item label="">
                  <el-button type="primary" @click="resaveAssessment()">
                    提交
                  </el-button>
                </el-form-item>
              </el-form>
            </template>
            <template v-else>
              <el-alert
                v-if="detail.status_text == '退回'"
                style="margin-bottom: 10px"
                type="error "
                effect="light"
                :show-icon="false"
                :closable="false"
              >
                <template #title> 退回中【 尚未重新提交 】 </template>
              </el-alert>
              <div class="desc">
                <label>自评报告：</label>
                <span>
                  <CommonUpload
                    :pid="detail.id"
                    tableName="performanceSelfAssessment"
                    useType="Self-Assessment-Report"
                    :showUploadBtn="false"
                  ></CommonUpload>
                </span>
              </div>
              <div class="desc">
                <label>备注：</label>
                <span>{{ detail.remark || "无" }}</span>
              </div>
            </template>
          </template>
          <template v-if="item.key == 2">
            <el-empty
              v-if="
                detail.status_text == '待审核' &&
                (pageType == 0 || pageType == 2)
              "
              :image-size="80"
              description="待审核"
            ></el-empty>

            <template v-if="detail.status_text == '待审核' && pageType == 1">
              <el-input
                style="margin-bottom: 10px"
                type="textarea"
                v-model="rejectReason"
                :autosize="{ minRows: 3, maxRows: 6 }"
                placeholder="退回原因，通过无需填写"
              ></el-input>

              <el-button type="danger" @click="save(15)">退回</el-button>
              <el-button type="success" @click="save(20)">通过</el-button>
            </template>

            <template v-if="detail.status_text == '退回'">
              <div v-for="ro in detail.opinion" :key="ro.id">
                <el-alert
                  style="margin-bottom: 10px"
                  type="error"
                  effect="light"
                  :show-icon="false"
                  :closable="false"
                >
                  <template #title>
                    审核退回【 {{ ro.dept_name }} 】 {{ ro.opdate }}
                  </template>
                  {{ ro.opinion }}
                </el-alert>
              </div>
            </template>

            <template
              v-if="
                detail.status_text == '通过' || detail.status_text == '已确认'
              "
            >
              <el-alert
                type="success"
                effect="light"
                show-icon
                :closable="false"
              >
                <template #title>
                  <b style="font-size: 16px; letter-spacing: 1px">
                    审核通过
                    {{ detail.examine_date }}
                  </b>
                </template>
              </el-alert>
            </template>
          </template>
          <template v-if="item.key == 3">
            <template v-if="pageType == 2 && detail.status_text == '通过'">
              <div class="desc">
                <label>绩效评价报告：</label>
                <CommonUpload
                  :pid="detail.id"
                  tableName="performanceEvaluation"
                  useType="Evaluation-Report"
                ></CommonUpload>
              </div>
              <el-button
                style="margin-bottom: 10px"
                type="success"
                @click="confirmPerformance"
              >
                确认
              </el-button>
            </template>
            <template v-if="detail.status_text == '已确认'">
              <el-alert
                style="margin-bottom: 10px"
                type="success"
                effect="light"
                show-icon
                :closable="false"
              >
                <template #title>
                  <b style="font-size: 16px; letter-spacing: 1px">
                    已确认
                    {{ detail.examine_date }}
                  </b>
                </template>
              </el-alert>
              <div class="desc">
                <label>绩效评价报告：</label>
                <CommonUpload
                  ref="peva"
                  :pid="detail.id"
                  tableName="performanceEvaluation"
                  useType="Evaluation-Report"
                  :disabled="true"
                  :showUploadBtn="false"
                ></CommonUpload>
              </div>
            </template>
            <template
              v-if="
                (pageType == 0 || pageType == 1) &&
                detail.status_text != '已确认'
              "
            >
              <el-empty :image-size="80" description="未确认"></el-empty>
            </template>
          </template>
        </div>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script>
import OperateCommon from "@/components/Common/OperateCommon.vue";
import AssessmentForm from "./AssessmentForm.vue";
import LinkTo from "@/mixins/LinkTo";
import CommonUpload from "@/components/Common/CommonUpload.vue";
import {
  getPerformanceSelfAssessmentDetail,
  postPerformanceSelfAssessmentDoneConfirm,
  postPerformanceSelfAssessmentReview,
  postPerformanceSelfAssessmentSave,
} from "@/api/WorkCollection/Index";

export default {
  name: "AssessmentDetail",
  mixins: [LinkTo],
  components: { AssessmentForm, OperateCommon, CommonUpload },
  data() {
    return {
      id: 0,
      pageType: 0,
      activeNames: ["0", "1", "2"],
      items: [
        {
          key: "0",
          title: "项目信息",
        },
        {
          key: "1",
          title: "绩效自评",
        },
        {
          key: "2",
          title: "自评审核",
        },
      ],
      detail: null,
      rejectReason: "",
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    initData() {
      getPerformanceSelfAssessmentDetail({ id: this.id }).then((res) => {
        if (res.code == 0) {
          this.detail = res.data;
          if (
            this.detail.status_text == "通过" ||
            this.detail.status_text == "已确认"
          ) {
            this.items.push({
              key: "3",
              title: "绩效评价",
            });
            this.activeNames.push("3");
          }
        }
      });
    },
    confirmPerformance() {
      if (this.$refs.peva.fileList.length == 0) {
        this.$message.error("请先上传评价报告");
        return;
      }
      postPerformanceSelfAssessmentDoneConfirm({
        id: this.detail.id,
        status: 30,
        status_text: "已确认",
      }).then((res) => {
        if (res.code == 0) {
          this.$message.success("确认成功");
          this.initData();
        }
      });
    },
    resaveAssessment() {
      if (this.$refs.upload[0].fileList.length == 0) {
        this.$message.error("请先上传自评报告！");
        return;
      }
      postPerformanceSelfAssessmentSave({
        id: this.detail.id,
        remark: this.detail.remark,
        status: 10,
        status_text: "待审核",
      }).then((res) => {
        if (res.code == 0) {
          this.$message.success("提交成功");
          this.initData();
        }
      });
    },
    save(status) {
      if (status == 15) {
        if (this.rejectReason.trim() == "") {
          this.$message.error("退回必须填写退回原因");
          return;
        }
        postPerformanceSelfAssessmentReview({
          id: this.detail.id,
          opinion: this.rejectReason,
          status: 15,
          status_text: "退回",
        }).then((res) => {
          if (res.code == 0) {
            this.$message.success("绩效自评退回成功");
            this.goBack();
          }
        });
      }
      if (status == 20) {
        postPerformanceSelfAssessmentReview({
          id: this.detail.id,
          opinion: "",
          status: 20,
          status_text: "通过",
        }).then((res) => {
          if (res.code == 0) {
            this.$message.success("绩效自评通过成功");
            this.goBack();
          }
        });
      }
    },
    tabsClick() {},
  },
  created() {
    this.pageType = this.$route.params.pageType;
    this.id = this.$route.params.id;

    this.initData();
  },
};
</script>

<style lang="scss" scoped>
</style>